﻿namespace TvUtil
{

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;

	public partial class MainForm : Form
	{
		private Timer m_timer;
		private static NotifyIcon _notify;
		private DateTime m_current = DateTime.MinValue;
		public MainForm()
		{
			InitializeComponent();
			this.m_timer = new Timer();
			this.m_timer.Interval = 1000;
			this.m_timer.Enabled = true;
			this.m_timer.Tick += new EventHandler(m_timer_Tick);
			_notify = this.niNotify;
			this.dtDate.MinDate = DateTime.Now.AddDays(-1);
			this.dtDate.MaxDate = DateTime.Now.AddDays(11);
		}
		public static NotifyIcon Notify
		{
			get
			{
				return _notify; 
			}
		}
		void m_timer_Tick(object sender, EventArgs e)
		{
			this.dtDate.MinDate = DateTime.Now.AddDays(-1);
			this.dtDate.MaxDate = DateTime.Now.AddDays(11);
			foreach (ProgramControl pc in this.tlp.Controls)
			{
				pc.CheckProgram();
			}
		}

		private void UpdateProgram(DateTime date)
		{

			ProgramStation[] st = ProgramStation.Create(date);
			this.tlp.SuspendLayout();
			this.tlp.Controls.Clear();
			this.tlp.ColumnStyles.Clear();
			foreach (ProgramStation station in st)
			{
				this.tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50.0F));
				ProgramControl pc = new ProgramControl(station);
				pc.Dock = DockStyle.Fill;
				this.tlp.Controls.Add(pc);
			}
			this.tlp.ResumeLayout(true);
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			this.UpdateProgram(DateTime.Now);
			this.m_timer.Start();
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			e.Cancel = true;
			this.Hide();
			base.OnClosing(e);
		}

		private void tsShowTVProgram_Click(object sender, EventArgs e)
		{
			this.Show();
			this.Activate();
		}

		private void tspUpdate_Click(object sender, EventArgs e)
		{
			this.UpdateProgram(this.dtDate.Value);
		}

		private void tspExit_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void dtDate_ValueChanged(object sender, EventArgs e)
		{
			if (dtDate.Value == this.m_current)
				return;
			this.UpdateProgram(this.dtDate.Value);
			this.m_current = dtDate.Value;
		}

	}
}
